import React from 'react';
import { Form, Radio, Row, Select, Input } from 'antd';
import styles from './index.less';
import RadioSelectFormItem from '../RadioSelect';
import SelectedTaskTable from './SelectedTaskTable';

export const LaunchRatingFormItems = props => {
    const { form, data, isDetail, userConfig, employeeList } = props;
    const { getFieldDecorator } = form;
    const tableData = data.taskList || data.taskInfoMap;
    const { taskArtificialScoreRuleType, artificialWeight } = data;
    const dataSource = Array.isArray(tableData) ? tableData : new Array(tableData);
    const taskIdList = dataSource.map((item, index) => (item && item.id) || index);

    const manRuleOptions = [{ label: '星级评分制', value: 0 }];

    const radioForm = (
        <Radio.Group>
            <Row>
                <Radio value="man">
                    发起人工评分，按
                    <span>
                        {getFieldDecorator('taskArtificialScoreRuleType', {
                            initialValue: 0
                        })(
                            <Select>
                                {manRuleOptions.map(item => (
                                    <Select.Option value={item.value}>{item.label}</Select.Option>
                                ))}
                            </Select>
                        )}
                    </span>
                    ; 权重占比：
                    <span className={styles.inlineFormItem}>
                        {getFieldDecorator('artificialWeight', {
                            initialValue: 100,
                            rules: [
                                {
                                    required: true,
                                    validator(_, value, callback) {
                                        if (!value) {
                                            return callback('请输入人工权重占比');
                                        }
                                        if (!/^1?\d{1,2}\.\d+$/.test(value) && !/^1?\d{1,2}$/.test(value)) {
                                            return callback('请输入0-100之间的数字');
                                        }
                                        if (!/^1?\d{1,2}(\.\d)?$/.test(value)) {
                                            return callback('最多保留一位有效数字');
                                        }
                                        const autoWeightValue = form.getFieldValue('autoWeight');
                                        if (Number(autoWeightValue) + Number(value) !== 100) {
                                            return callback('评分类型权重占比需=100%');
                                        }
                                        return callback();
                                    }
                                }
                            ]
                        })(<Input suffix="%"></Input>)}
                    </span>
                </Radio>
            </Row>
            <Row>
                <Radio value="auto" disabled>
                    发起自动化评分，按
                    <span>
                        <Select placeholder="请选择" disabled></Select>
                    </span>
                    ; 权重占比：
                    <span>
                        {getFieldDecorator('autoWeight', {
                            initialValue: 0
                        })(<Input disabled suffix="%"></Input>)}
                    </span>
                </Radio>
            </Row>
        </Radio.Group>
    );

    const getDisplayText = () => {
        // 人工评分
        const manRuleText =
            ((taskArtificialScoreRuleType || taskArtificialScoreRuleType === 0) &&
                manRuleOptions.find(item => item.value === taskArtificialScoreRuleType).label) ||
            '';
        return <span>{`发起人工评分，按${manRuleText}；权重占比${artificialWeight}%`}</span>;
    };

    return (
        <>
            <Form.Item label="评分类型" className={styles.scoreType} required key="assessType">
                {getFieldDecorator('assessType', { initialValue: 'man' })(isDetail ? getDisplayText() : radioForm)}
            </Form.Item>
            <RadioSelectFormItem
                form={form}
                label="评分人"
                isDetail={isDetail}
                data={data}
                userConfig={userConfig}
                employeeList={employeeList}
            />
            <div>已选任务清单</div>
            <Form.Item key="taskIds" wrapperCol={{ span: 24 }}>
                {getFieldDecorator('taskIds', { initialValue: taskIdList })(
                    <SelectedTaskTable dataSource={dataSource} />
                )}
            </Form.Item>
        </>
    );
};

export const MoveToNoRateFormItems = props => {
    const { form, data: receivedData, confirmText = '确定对以下任务移入无需评分？' } = props;
    const tableData = receivedData.taskList || receivedData.taskInfoMap;
    const data = Array.isArray(tableData) ? tableData : new Array(tableData);
    const taskIdList = data.map((item, index) => (item && item.id) || index);
    return (
        <>
            <div className={styles.reCheckTitle}>{confirmText}</div>
            {form.getFieldDecorator('taskIds', { initialValue: taskIdList })(<SelectedTaskTable dataSource={data} />)}
        </>
    );
};
